package com.link.base.base.pageurls.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.link.base.base.autoconfig.nocode.model.LnkInnerCode;
import com.link.base.base.pageurls.dao.mybatis.mapper.PageUrlsMapper;
import com.link.base.base.pageurls.model.PageUrls;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 页面url信息
 *
 * @author 秦学刚
 * @date 2020-07-03 14:54:18
 */
@Service
public class PageUrlsServiceImpl extends BasicServiceImpl<PageUrls> implements PageUrlsService {

    @Resource
    private PageUrlsMapper    pageUrlsMapper;

    @Override
    public BasicMapper<PageUrls> getBasicMapper() {
        return pageUrlsMapper;
    }

    @Override
    public List<PageUrls> queryByPagePath(PageUrls pageUrls) throws Exception {
        return pageUrlsMapper.queryByPagePath(pageUrls);
    }

    @Override
    public PageUrls queryExistUrls(PageUrls urls) {
        return pageUrlsMapper.queryExistUrls(urls);
    }

    @Override
    public void insertUrls(LnkInnerCode entity) throws Exception {
        String entityName = entity.getEntityName();

        String urlJson =
             "[{\"effective\":\"Y\",\"pagePath\":\"PAGEPATH\",\"url\":\"/nocode/ENTITYNAME/queryById\",\"urlType\":\"查询\",\"remarks\":\"标准-基于ID查询\"},"
            + "{\"effective\":\"Y\",\"pagePath\":\"PAGEPATH\",\"url\":\"/nocode/ENTITYNAME/queryByExamplePage\","
                     + "\"urlType\":\"查询\",\"remarks\":\"标准-分页查询\"},"
            + "{\"effective\":\"Y\",\"pagePath\":\"PAGEPATH\",\"url\":\"/nocode/ENTITYNAME/upsert\","
                     + "\"urlType\":\"更新\",\"remarks\":\"标准-新建或更新\"},"
            + "{\"effective\":\"Y\",\"pagePath\":\"PAGEPATH\",\"url\":\"/nocode/ENTITYNAME/insert\","
                     + "\"urlType\":\"新建\",\"remarks\":\"标准-新建\"},"
            + "{\"effective\":\"Y\",\"pagePath\":\"PAGEPATH\",\"url\":\"/nocode/ENTITYNAME/batchInsert\","
                     + "\"urlType\":\"新建\",\"remarks\":\"标准-批量更新\"},"
            + "{\"effective\":\"Y\",\"pagePath\":\"PAGEPATH\",\"url\":\"/nocode/ENTITYNAME/preDefaultValue\","
                     + "\"urlType\":\"新建\",\"remarks\":\"标准-获取新建默认值\"},"
            + "{\"effective\":\"Y\",\"pagePath\":\"PAGEPATH\",\"url\":\"/nocode/ENTITYNAME/update\","
                     + "\"urlType\":\"更新\",\"remarks\":\"标准-更新\"},"
            + "{\"effective\":\"Y\",\"pagePath\":\"PAGEPATH\",\"url\":\"/nocode/ENTITYNAME/batchUpdate\","
                     + "\"urlType\":\"更新\",\"remarks\":\"标准-批量更新\"},"
            + "{\"effective\":\"Y\",\"pagePath\":\"PAGEPATH\",\"url\":\"/nocode/ENTITYNAME/deleteById\","
                     + "\"urlType\":\"删除\",\"remarks\":\"标准-基于ID删除\"},"
            + "{\"effective\":\"Y\",\"pagePath\":\"PAGEPATH\",\"url\":\"/nocode/ENTITYNAME/batchDelete\","
                     + "\"urlType\":\"删除\",\"remarks\":\"标准-基于ID查询\"},"
            + "{\"effective\":\"Y\",\"pagePath\":\"PAGEPATH\",\"url\":\"/nocode/ENTITYNAME/queryByExamplePage/exportData\","
                     + "\"urlType\":\"导出\",\"remarks\":\"标准-导出数据\"},"
            + "{\"effective\":\"Y\",\"pagePath\":\"PAGEPATH\",\"url\":\"/nocode/ENTITYNAME/importData\","
                     + "\"urlType\":\"新建\",\"remarks\":\"标准-导入数据\"},"
            + "{\"effective\":\"Y\",\"pagePath\":\"PAGEPATH\",\"url\":\"/nocode/ENTITYNAME/queryByExamplePage/exportExcel\","
                     + "\"urlType\":\"导出\",\"remarks\":\"标准-导出excel\"}]";
        String newUrlJson = urlJson.replaceAll("ENTITYNAME",StringUtils.toCalmelCase(entityName))
                .replaceAll("PAGEPATH","[" + entity.getModelPath() + "]");

        Gson gson = new Gson();
        List<PageUrls> pageUrlsList = gson.fromJson(newUrlJson, new TypeToken<List<PageUrls>>() {}.getType());
        for (PageUrls url : pageUrlsList) {
            PageUrls existUrls = queryExistUrls(url);
            if (existUrls == null) {
                insert(url);
            }
        }
    }

}
